﻿using CRMG.Container.Resources.ExceptionResources;
using CRMG.Container.Source.BO;
using CRMG.Container.Source.Helper;
using CRMG.Container.Source.OpManager;
using CRMG.Container.Source.Variables;
using Guru.ErrorHandling;
using Guru.TransactionLogger;
using Guru.TransactionLogger.Source.BO;
using System;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Threading;
using Telerik.WinControls.UI;

namespace CRMG.Container.Views.Training
{
    public partial class FrmTrain : RadForm
    {
        ResourceManager formRes;
        ResourceManager messageRes;
        TrainManager trainMan;
        LookupManager lookupMan;
        Train pTrain;
        int pTrainId = -1;

        public FrmTrain() : this(-1) { }

        public FrmTrain(int trainId)
        {
            try
            {
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(AppVariables.ApplicationCulture);
                pTrain = new Train();
                InitializeComponent();
                formRes = new ResourceManager("CRMG.Container.Resources.ControlResources.WinFormStrings", typeof(FrmTrain).Assembly);
                messageRes = new ResourceManager("CRMG.Container.Resources.MessageResources.AppMessages", typeof(FrmTrain).Assembly);
                trainMan = new TrainManager();
                lookupMan = new LookupManager();
                pTrainId = trainId;

                this.Text = formRes.GetString("FrmTrain.Caption");
                lblTrainName.Text = formRes.GetString("FrmTrain.lblTrain");
                lblDefinition.Text = formRes.GetString("FrmTrain.lblDefinition");
                lblVendor.Text = formRes.GetString("FrmTrain.lblVendor");
                btnSave.Text = pTrainId == -1 ? formRes.GetString("Add") : formRes.GetString("Update");
                btnCancel.Text = formRes.GetString("Cancel");
                ControlUtil.LoadDataTable2DropDownList(drpVendor, lookupMan.GetVendors(), "VendorName", "OBJID");

                if (pTrainId != -1)
                {
                    pTrain = trainMan.GetTrainById(pTrainId);
                    txtTrainName.Text = pTrain.TrainName;
                    txtDefinition.Text = pTrain.Definiton;
                    drpVendor.SelectedValue = pTrain.Vendor;
                }
            }
            catch (GuruException)
            {
                ControlUtil.ShowErrorMessage(
                          messageRes.GetString("FrmTrain.Ctor.Error"),
                          messageRes.GetString("Error"));
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2024,
                    Message = exc.Message,
                    StackTrace = exc.StackTrace,
                    Title = Rc.GetString(2024)
                };
                logger.Write();
                ControlUtil.ShowErrorMessage(
                      messageRes.GetString("FrmTrain.Ctor.Error"),
                      messageRes.GetString("Error"));
            }
            finally
            {
                btnSave.Enabled = false;
                pTrain = new Train();

                if (pTrainId != -1)
                    pTrain.OBJID = pTrainId;
            }
        }

        private void TrainChange(object sender, EventArgs e)
        {
            btnSave.Enabled = txtTrainName.Text.Length > 0;
            pTrain.TrainName = txtTrainName.Text;
        }

        private void DefinitionChange(object sender, EventArgs e)
        {
            pTrain.Definiton = txtDefinition.Text;
        }


        private void drpVendor_SelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
        {
            pTrain.Vendor = drpVendor.SelectedValue.ToInt();
        }

        private void Save(object sender, EventArgs e)
        {
            try
            {
                if (drpVendor.SelectedValue.ToInt() == 0)
                {
                    ControlUtil.ShowWarningMessage(
                     messageRes.GetString("FrmTrain.Save.EmptyVendor"),
                     messageRes.GetString("Warning"));
                    return;
                }
                else
                {
                    Train trn = trainMan.GetTrainByVendorAndName(pTrain.TrainName, pTrain.Vendor);
                    if (trn != null)
                    {
                        if (trn.OBJID != pTrainId)
                        {
                            ControlUtil.ShowWarningMessage(
                     messageRes.GetString("FrmTrain.Save.TrainAlreadyExist"),
                     messageRes.GetString("Warning"));
                            return;
                        }
                    }

                    int Id = -1;
                    if (pTrainId == -1)
                    {
                        Id = trainMan.AddTrain(pTrain);
                    }
                    else
                    {
                        if (pTrain.GetColumnChangeList().Count > 1)
                        {
                            trainMan.UpdateTrain(pTrain);
                        }
                    }

                    TransactionLogEntry transLogger = new TransactionLogEntry(AppVariables.CurrentUser.OBJID)
                    {
                        FormName = this.Name,
                        TransactionType = pTrainId == -1 ? TransactionTypes.Insert : TransactionTypes.Update,
                        ControlName = btnSave.Text,
                        ControlTag = btnSave.Tag != null ? btnSave.Tag.ToString() : "",
                        LogObject = pTrainId == -1 ? Id : pTrainId
                    };
                    transLogger.Insert();

                    btnSave.Enabled = false;
                    btnCancel.Text = formRes.GetString("Close");
                    ControlUtil.ShowInfoMessage(
                      messageRes.GetString("FrmTrain.Save.Success"),
                      messageRes.GetString("Info"));
                }
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2025,
                    Message = exc.Message,
                    StackTrace = exc.StackTrace,
                    Title = Rc.GetString(2025)
                };
                logger.Write();
                ControlUtil.ShowErrorMessage(
                      messageRes.GetString("FrmTrain.Save.Error"),
                      messageRes.GetString("Error"));
            }
        }

        private void Cancel(object sender, EventArgs e)
        { this.Close(); }

    }
}
